package com.base.cn.platform.os.controller.mobile.user.center.userinfo;

import com.base.cn.platform.os.common.MobileBaseController;
import com.base.cn.platform.os.common.constants.CommonConstants;
import com.base.cn.platform.os.common.j2cache.CustomCacheChannel;
import com.base.cn.platform.os.common.rsa.WebRsaUtils;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.common.utils.web.WebUtils;
import com.base.cn.platform.os.entity.manage.area.SysArea;
import com.base.cn.platform.os.service.manage.area.SysAreaService;
import com.base.cn.platform.os.service.manage.user.address.CusUserAddressService;
import com.base.cn.platform.os.service.manage.user.login.UserLoginService;
import com.base.cn.platform.os.service.manage.user.user.CusUserService;
import io.netty.util.internal.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * H5个人信息Controller
 *
 * @author zsw
 * @create 2018/6/25
 */
@Controller
@RequestMapping("/mobile/uc/userInfo/")
public class MobileUserInfoController extends MobileBaseController {

    @Autowired
    private CusUserService userService;
    @Autowired
    private UserLoginService userLoginService;
    @Autowired
    private CustomCacheChannel customCacheChannel;
    @Autowired
    private CusUserAddressService cusUserAddressService;
    @Autowired
    private SysAreaService sysAreaService;

    /**
     * 修改用户昵称
     *
     * @param request
     * @param nickname
     * @return
     */
    @RequestMapping("/updateUserNickname")
    public String updateUserNickname(HttpServletRequest request,
                                     @RequestParam("nickname") String nickname) {
        request.setAttribute("nickname", nickname);
        return resultPage("/user/center/userInfo/u-update-nickname");
    }

    /**
     * 修改用户真实姓名
     *
     * @param request
     * @param realName
     * @return
     */
    @RequestMapping("/updateUserRealName")
    public String updateUserRealName(HttpServletRequest request,
                                     @RequestParam("realName") String realName) {
        request.setAttribute("realName", realName);
        return resultPage("/user/center/userInfo/u-update-realName");
    }

    /**
     * 修改用户个性签名
     *
     * @param request
     * @param userInfo
     * @return
     */
    @RequestMapping("/updateUserSignature")
    public String updateInfo(HttpServletRequest request,
                             @RequestParam("userInfo") String userInfo) {
        request.setAttribute("userInfo", userInfo);
        return resultPage("/user/center/userInfo/u-update-signature");
    }

    /**
     * 修改用户密码
     *
     * @param request
     * @return
     */
    @RequestMapping("/updateUserPassword")
    public String updateUserPassword(HttpServletRequest request) throws Exception {
        Map<String,String> keyMap = WebRsaUtils.getRSAData(customCacheChannel);
        request.setAttribute("keyMap",keyMap);
        return resultPage("/user/center/userInfo/u-update-password");
    }

    /**
     * 修改用户密码
     *
     * @param request    HttpServletRequest
     * @param password     原密码
     * @param newPassword     新密码
     * @param confirmPwd 确认密码
     * @return
     */
    @RequestMapping("/updatePassword")
    @ResponseBody
    public Map<String, Object> updatePassword(HttpServletRequest request,
                                              @RequestParam(value = "password") String password,
                                              @RequestParam(value = "newPassword") String newPassword,
                                              @RequestParam(value = "confirmPwd") String confirmPwd,
                                              @RequestParam("sign") String sign) throws Exception{
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        Map<String,Object> resultMap = userService.updateUserPwd(userId, password, newPassword, confirmPwd,sign);
        if(!ResultUtil.getSuccess(resultMap)){
            Map<String,String> keyMap = WebRsaUtils.getRSAData(customCacheChannel);
            resultMap.put("keyMap",keyMap);
        }
        return resultMap;
    }

    /**
     * 修改用户信息
     *
     * @param user 用户对象
     * @return Map<String,Object>
     */
    @RequestMapping("/updateUserInfo")
    @ResponseBody
    public Map<String, Object> updateUserInfo(HttpServletRequest request,
                                              @RequestBody Map<String,Object> user) {
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        user.put("id",userId);
        Map<String,Object> resultMap = userService.updateUser(user);
        if(ResultUtil.getSuccess(resultMap)){
            String strUser = (String)ResultUtil.getEntity(resultMap);
            String sid = WebUtils.getCookie(request, CommonConstants.MOBILE_USER_SINGLE_ID);
            //重新更新缓存
            userLoginService.updateUserLoginCache(sid,strUser);
        }
        return resultMap;
    }

    /**
     * 收货地址
     * @return
     */
    @RequestMapping("/updateUserAddress")
    public String updateUserAddress(HttpServletRequest request, Model model){
        BigDecimal loginUserId = webMobileLoginUtil.getLoginUserId(request);
        List<Map<String, Object>> maps = cusUserAddressService.queryUserAddressListByUserId(loginUserId, true);
         model.addAttribute("address",maps);
        return resultPage("/user/center/userInfo/u-update-address");
    }
    /**
     * 收货地址
     * @return
     */
    @RequestMapping("/setUserAddressDefault")
    @ResponseBody
    public Map<String, Object>  setUserAddressDefault(@RequestParam("id") BigDecimal addressId,
                                                  @RequestParam("status") Integer status){
        if (status==2) {
            return ResultUtil.ERROR("必须设置一个默认地址", null);
        }
         cusUserAddressService.updateUserAddressIsFirst(addressId, status);
        return ResultUtil.SUCCESS("操作成功", null);
    }
    /**
     * 设置收货地址
     * @return
     */
    @RequestMapping("/addUserAddress")
    public String addUserAddress(Model model,@RequestParam(value =  "id",defaultValue = "0",required =  false) BigDecimal addressId){

        Map objectObjectHashMap = new HashMap<>();
        if(DataUtil.idIsNotNull(addressId)){
            objectObjectHashMap = cusUserAddressService.queryUserAddressById(addressId, true);
        }
        model.addAttribute("entity",objectObjectHashMap);
        return resultPage("/user/center/userInfo/u-add-address");
    }

    /**
     * 删除用户地址
     *
     * @param addressId 地址Id
     * @return
     */
    @RequestMapping("/deleteUserAddressById")
    @ResponseBody
    public Map<String, Object> deleteUserAddressById(@RequestParam("id") BigDecimal addressId) {
        return  cusUserAddressService.deleteUserAddressById(addressId);
    }




    /**
     * 保持修改地址
     * @param request
     * @param addUserAddress
     * @return
     */
    @RequestMapping("/initAddUserAddress")
    @ResponseBody
    public Map<String, Object> initAddUserAddress(HttpServletRequest request,
                                              @RequestBody Map<String,Object> addUserAddress) {
        BigDecimal loginUserId = webMobileLoginUtil.getLoginUserId(request);
            addUserAddress.put("userId",loginUserId);
         if(ObjectUtils.isNotEmpty(addUserAddress.get("citys"))){
             setAddress(addUserAddress);
         }else{
             return  ResultUtil.ERROR("请选择地区");
         }
        return cusUserAddressService.saveUpdateCusUserAddress(addUserAddress);
    }


    /**
     * 收货地址
     * @return
     */
    @RequestMapping("/getUserAddress")
    public String getUserAddress(HttpServletRequest request, Model model){
        BigDecimal loginUserId = webMobileLoginUtil.getLoginUserId(request);
        List<Map<String, Object>> maps = cusUserAddressService.queryUserAddressListByUserId(loginUserId, true);
        model.addAttribute("address",maps);
        return resultPage("/user/center/userInfo/u-update-address");
    }

    /**
     * 处理地址信息
     * @param addUserAddress
     */
    private  void setAddress(Map<String,Object> addUserAddress){
        String citys = addUserAddress.get("citys").toString();
        String[] split = citys.split("-");
        for(int i=0;i<split.length;i++){
            SysArea sysArea = sysAreaService.queryFullAddress(i, split[i].trim());
            if(i==0){
                addUserAddress.put("provinceId",sysArea.getId());
                continue;
            }
            if(ObjectUtils.isNotEmpty(sysArea)||i==1){
                Map map1 = null;
              if(!ObjectUtils.isNotEmpty(sysArea)&&i==1){
                  sysArea = sysAreaService.queryFullAddress(i+1,split[i].trim());
                }
                if(i==1){
                    addUserAddress.put("cityId",sysArea.getId());
                }
                if(i==2){
                    addUserAddress.put("townId",sysArea.getId());
                }
            }
        }
    }


    /**
     * 收货地址
     * @return
     */
    @RequestMapping("/getListAddressAjax")
    @ResponseBody
    public   List<Map<String, Object>>  getListAddressAjax(HttpServletRequest request, Model model){
        BigDecimal loginUserId = webMobileLoginUtil.getLoginUserId(request);
       return  cusUserAddressService.queryUserAddressListByUserId(loginUserId, true);

    }

}
